#!/usr/bin/env python
# -*- coding:utf-8 -*-

from django import forms
from cmdb import models
import re
from django.core.exceptions import ValidationError

class LoginForm(forms.Form):
    username = forms.CharField(
        widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': u'用户名'})
    )
    password = forms.CharField(
        widget=forms.PasswordInput(attrs={'class': "form-control", 'placeholder': u'密码'})
    )

def validate_ipv4(value):
    ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
    if not ipv4_re.match(value):
        raise ValidationError('管理IP格式错误.')

class SampleImportForm(forms.Form):
    sn = forms.CharField(
        error_messages={'required': u'SN号不能为空.'},
        widget=forms.TextInput(attrs={'class': "form-control no-radius", 'placeholder': u'SN号'})
    )
    hostname = forms.CharField(
        error_messages={'required': u'主机名不能为空.'},
        widget=forms.TextInput(attrs={'class': "form-control no-radius", 'placeholder': u'主机名'})
    )
    manage_ip = forms.CharField(
        validators=[validate_ipv4,],    #根据正则判断IP
        error_messages={'required': u'管理卡不能为空.'},
        widget=forms.TextInput(attrs={'class': "form-control no-radius", 'placeholder': u'管理卡'})
    )

    def __init__(self, *args, **kwargs):
        super(SampleImportForm, self).__init__(*args, **kwargs)

class TableForm(forms.Form):
    select=forms.CharField(
        widget=forms.CheckboxInput()
    )
    status=forms.IntegerField(
        # widget=forms.widgets.Select(choices=[{'id':1,'text':'online'},{'id':2,'text':'offline'}])
        widget=forms.widgets.Select(choices=[(1,'online'),(2,'offline')])
    )